package com.goodfox.web.generic.role.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.generic.provide.annotation.NoteExplain;
import com.goodfox.web.generic.provide.entity.Pager;
import com.goodfox.web.generic.provide.util.ParameterUtil;
import com.goodfox.web.generic.provide.util.UUIDUtil;
import com.goodfox.web.generic.role.entity.Role;
import com.goodfox.web.generic.role.entity.RoleFunction;
import com.goodfox.web.generic.role.mapper.RoleMapper;
import com.goodfox.web.generic.role.service.RoleService;

@Transactional
@Service(value="roleService")
@NoteExplain(comment="角色权限管理")
public class RoleServiceImpl implements RoleService {

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(RoleServiceImpl.class);

	/** 角色Mapper */
	@Autowired
	private RoleMapper roleMapper;

	@NoteExplain(comment="查询角色列表")
	public Pager<Role> queryRolePager(int currentPage,int numPerPage,Map map){
		int start=(currentPage-1)*numPerPage;
		Long allCount=roleMapper.selectRoleListCount(map);
		List<Role> list=roleMapper.selectRoleList(map,new RowBounds(start, numPerPage));
		return new Pager<Role>(allCount,list);
	}

	@NoteExplain(comment="获取角色")
	public Role getRole(String roleId){
		return roleMapper.selectRoleById(roleId);
	}

	@NoteExplain(comment="新增角色",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addRole(Role entity,String loginLogId){
		entity.setRolePower("USER_"+entity.getRoleId());
		entity.setRoleState("1");
		roleMapper.insertRole(entity);
		List<String> funIdsList=ParameterUtil.getListForString(entity.getFunIds(), ";");
		if(funIdsList.size()>0){
			for(String funId:funIdsList){
				RoleFunction roleFun=new RoleFunction();
				roleFun.setRoleFunId(UUIDUtil.getUUID());
				roleFun.setRoleId(entity.getRoleId());
				roleFun.setFunId(funId);
				roleMapper.insertRoleFunction(roleFun);
			}
//			roleMapper.deleteRoleFunctionNoVisit(entity);
		}
	}

	@NoteExplain(comment="更新角色",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyRole(Role oldEny,Role newEny,String loginLogId){
		roleMapper.updateRole(newEny);
		roleMapper.deleteRoleFunctionById(newEny.getRoleId());
		List<String> funIdsList=ParameterUtil.getListForString(newEny.getFunIds(), ";");
		if(funIdsList.size()>0){
			for(String funId:funIdsList){
				RoleFunction roleFun=new RoleFunction();
				roleFun.setRoleFunId(UUIDUtil.getUUID());
				roleFun.setRoleId(newEny.getRoleId());
				roleFun.setFunId(funId);
				roleMapper.insertRoleFunction(roleFun);
			}
//			roleMapper.deleteRoleFunctionNoVisit(newEny);
		}
	}

	
	@NoteExplain(comment="移除角色",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeRole(Role entity,String loginLogId){
		roleMapper.deleteRoleFunctionById(entity.getRoleId());
		roleMapper.deleteRoleById(entity.getRoleId());
	}

	@NoteExplain(comment="查询角色列表")
	public List<Role> queryRoleList(Map map){
		return roleMapper.selectRoleList(map);
	}
	
	@NoteExplain(comment="判断角色是否存在")
	public boolean isHave(String roleName){
		boolean b=false;
		try{
			Role role=roleMapper.selectRoleByName(roleName);
			if(null!=role){
				b=true;
			}
		}catch(Exception ex){
			logger.error("根据角色名获取角色异常：",ex);
		}
		return b;
	}
	
	@NoteExplain(comment="判断角色是否有绑定员工")
	public boolean isBindingAccount(String roleId){
		boolean b=false;
		Long count=roleMapper.selectRoleAccountCount(roleId);
		if(count>0){
			b=true;
		}
		return b;
	}
}
